package com.ipzoe.app.core.interceptor;

import com.ipzoe.app.core.constants.AppConstants;
import com.ipzoe.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 获取微信openid
 */
@Component
public class WxOpenidInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {

        // 获取openid
        String openid = request.getHeader(AppConstants.OPENID_KEY);
        if (StringUtils.isBlank(openid)) {
            openid = request.getParameter(AppConstants.OPENID_KEY);
        }

        // 设置openid到request里
        request.setAttribute(AppConstants.REQUEST_ATTRIBUTE_KEY_OPENID, openid);

        return true;
    }
}
